//
//  ChatConsoleView.m
//  weibo
//
//  Created by wudongyang on 16/6/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ChatConsoleView.h"
#import "EMCDDeviceManager.h"
#import "BQCollectionViewCell.h"
@interface ChatConsoleView ()<UITextFieldDelegate>



@property (nonatomic, retain)UIButton *recoderBtn;

@end

@interface ChatConsoleView ()<UITextFieldDelegate>

@end

@implementation ChatConsoleView

//@synthesize draftTextField, sendButton;
- (void)dealloc
{
    Block_release(_buttonClicked);
    
    
    [_recoderBtn release];
    [_draftTextField release];
    [_sendButton release];
    [_voiceButton release];
    [_expressionButton release];
    [_addButton release];
    
    [super dealloc];
}
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupWithFrame:frame];
        
    }
    return self;

}

-(void)setupWithFrame:(CGRect)frame{
    self.backgroundColor = DSRGBA(246, 246, 246, 1);
    self.layer.borderWidth = 0.5;
    self.layer.borderColor = DSRGBA(184, 184, 184, 1).CGColor;
    self.draftTextField = [[UITextField alloc] initWithFrame:CGRectMake(40, 5, frame.size.width - 120, frame.size.height - 10)];
    [self.draftTextField setBorderStyle:(UITextBorderStyleRoundedRect)];
    self.draftTextField.placeholder = @"说点儿啥呢?";
    self.draftTextField.layer.masksToBounds = YES;
    self.draftTextField.layer.cornerRadius = 4;
    self.draftTextField.layer.borderWidth = 1;
    self.draftTextField.layer.borderColor = DSRGBA(184, 184, 184, 1).CGColor;
    // 输入框编辑文字一建删除
    self.draftTextField.clearButtonMode = UITextFieldViewModeAlways;
    // 光标颜色等于文字颜色
    self.tintColor = self.draftTextField.textColor;
    self.draftTextField.delegate = self;
    [self.draftTextField setFont:[UIFont systemFontOfSize:13]];
    [self addSubview:_draftTextField];
    [_draftTextField release];
    
    
    // 创建语音按钮
    self.voiceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.voiceButton.backgroundColor = DSRGBA(246, 246, 246, 1);
    [self.voiceButton setImage:[UIImage imageNamed:@"chat_bottom_voice_nor"] forState:UIControlStateNormal];
    [self.voiceButton setImage:[UIImage imageNamed:@"chat_bottom_voice_press"] forState:UIControlStateHighlighted];
    self.voiceButton.layer.masksToBounds = YES;
    self.voiceButton.layer.cornerRadius = (frame.size.height - 13) / 2;
    self.voiceButton.layer.borderWidth = 1;
    self.voiceButton.layer.borderColor = DSRGBA(124, 124, 124, 1).CGColor;
    [self addSubview:self.voiceButton];
    [self.voiceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self).offset(7);
        make.size.mas_equalTo(CGSizeMake(frame.size.height - 13, frame.size.height - 13));
        make.bottom.equalTo(self).offset(-7);
        make.right.equalTo(self.draftTextField.mas_left).offset(-7);
        
    }];

    [self.voiceButton addTarget:self action:@selector(voiceButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.recoderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.recoderBtn.frame = self.draftTextField.frame;
    self.recoderBtn.backgroundColor = [UIColor redColor];

    // 创建表情按钮
    self.expressionButton = [UIButton buttonWithType:UIButtonTypeCustom];

    self.expressionButton.backgroundColor = DSRGBA(246, 246, 246, 1);
    [self.expressionButton setImage:[UIImage imageNamed:@"chat_bottom_smile_nor"] forState:UIControlStateNormal];
    [self.expressionButton setImage:[UIImage imageNamed:@"chat_bottom_smile_press"] forState:UIControlStateHighlighted];
    [self addSubview:self.expressionButton];
    [self.expressionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(frame.size.height - 13, frame.size.height - 13));
        make.top.equalTo(self).offset(0);
        make.bottom.equalTo(self).offset(0);
        make.right.equalTo(self).offset(-40);
        make.left.equalTo(self.draftTextField.mas_right).offset(5);
        
    }];
    [self.expressionButton addTarget:self action:@selector(expressionButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 创建加号按钮
    self.addButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.expressionButton.backgroundColor = DSRGBA(246, 246, 246, 1);
    [self.addButton setImage:[UIImage imageNamed:@"chat_bottom_up_nor"] forState:UIControlStateNormal];
    [self.addButton setImage:[UIImage imageNamed:@"chat_bottom_up_press"] forState:UIControlStateHighlighted];
    [self addSubview:self.addButton];
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(frame.size.height - 13, frame.size.height - 13));
        make.top.equalTo(self).offset(0);
        make.bottom.equalTo(self).offset(0);
        make.right.equalTo(self).offset(0);
        make.left.equalTo(self.expressionButton.mas_right).offset(0);
        
    }];
    

    
}



- (void)voiceButtonAction:(UIButton *)button {
    if (button.selected == NO) {
//        NSLog(@"%@",button1);
        if (![self.subviews containsObject:_recoderBtn]) {
            NSLog(@"sdfsdf");
            [self addSubview:self.recoderBtn];
            NSLog(@"%@",self.recoderBtn);
            [self.recoderBtn addTarget:self action:@selector(btnDownAction:) forControlEvents:UIControlEventTouchDown];
            [self.recoderBtn addTarget:self action:@selector(btnUpAction:) forControlEvents:UIControlEventTouchUpInside];
        }
        
    } else {
        
        [self.recoderBtn removeFromSuperview];
    }
    button.selected = !button.selected;
//    [button addTarget:self action:@selector(btnDownAction:) forControlEvents:UIControlEventTouchDown];
//    [button addTarget:self action:@selector(btnUpAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)btnDownAction:(UIButton *)button {
    int x = arc4random() % 100000;
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
    NSString *fileName = [NSString stringWithFormat:@"%d%d",(int)time,x];
    [[EMCDDeviceManager sharedInstance] asyncStartRecordingWithFileName:fileName completion:^(NSError *error) {
        // 文件名以时间命名
        if (!error) {
            NSLog(@"开始录音");
        }
        
        
    }];
}

- (void)btnUpAction:(UIButton *)btn {
    NSLog(@"sdfhsdhfsdkfsdfhsdfhdsfds");
    [[EMCDDeviceManager sharedInstance] asyncStopRecordingWithCompletion:^(NSString *recordPath, NSInteger aDuration, NSError *error) {
        NSLog(@"%@",recordPath);
        EMChatVoice *voice = [[EMChatVoice alloc] initWithFile:recordPath displayName:@"[语音]"];
        voice.duration = aDuration;
        EMVoiceMessageBody *body = [[EMVoiceMessageBody alloc] initWithChatObject:voice];
        EMMessage *message = [[EMMessage alloc] initWithReceiver:self.chatter bodies:@[body]];
        [[[EaseMob sharedInstance] chatManager] asyncSendMessage:message progress:nil prepare:^(EMMessage *message, EMError *error) {
            NSLog(@"准备发送");
            
        } onQueue:dispatch_get_main_queue() completion:^(EMMessage *message, EMError *error) {
            
            NSLog(@"发送成功");
        } onQueue:nil];
    }];
}
// 点击聊天页面的发送按钮
//-(void)didSendButtonClicked:(UIButton *)sender{
//
//    if (self.buttonClicked) {
//        self.buttonClicked(self.draftTextField.text);
//    }
//    self.draftTextField.text = @"";
//
//}
-(NSString *)draftText {
    return self.draftTextField.text;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    NSLog(@"1-1-1-1-1-1-1");
    if (self.buttonClicked) {
        self.buttonClicked(self.draftTextField.text);
    }
//    [textField resignFirstResponder];
    self.draftTextField.text = @"";
//    self.isBool = NO;
    return YES;
    
}


-(void)expressionButtonAction:(UIButton *)button{
    
    if (self.isBool) {
        NSLog(@"dsfsd");
        [self.draftTextField becomeFirstResponder];// 弹键盘
        self.isBool = YES;
    } else {
        NSLog(@"sdkfsdklf");
        [self.draftTextField resignFirstResponder];
        self.BQButtonBlock(button);
        self.isBool = NO;
    
    }
    
    self.isBool = !self.isBool;

}
// textField可编辑时执行
- (BOOL)textFieldShouldClear:(UITextField *)textField{
    
    return YES;
    
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"111");
//    self.isBool = NO;
    self.isBool = !self.isBool;

}



@end
